<template>
    <div class="body" style=" height: 800px;z-index:5;position:absolute;top:0px;">
        <main>
            <div class="container">
                <div class="right">
                    <div class="top">AI问答</div>
                    <div class="chat" ref="chatContainer">
                        <div v-for="(item, i) in msgList" :key="i" :class="item.type == '1' ? 'rightMsg' : 'leftMsg'">
                            <img v-if="item.type == '0'" src="@/assets/机器人.gif" alt="" />
                            <!-- 回答的答案 -->
                            <div class="msg">{{ item.content }}</div>
                            <img v-if="item.type == '1'" :src="store.state.UserInformation.Head" alt="" />
                            <!-- 我问的问题 -->
                        </div>
                    </div>
                    <div class="bottom">
                        <input v-model="value" placeholder="请输入您想提问的内容" />
                        <button @click="sendQuestion">
                            <img src="@/assets/send.png"
                                style="width:40px;height:40px;position:relative;top:3px;left:-25px;" />
                            <h6 style="font-size:15px;color:white;position: relative;top:-32px;left:8px;">发送</h6>
                        </button>
                    </div>
                </div>
            </div>
        </main>
    </div>
    <foot></foot>
</template>

<script setup>
import { ref, reactive, nextTick, onMounted } from "vue";

import { useStore } from 'vuex';
const store = useStore();
const value = ref("");//输入的问题
const msgList = reactive([
    {
        type:'0',
        content:'你想问我什么呢?'
    }
]);//聊天记录
import { serverAddress } from '@/config'
// 等待DOM更新完成。自动滚动到最新发送的消息处
const scrollToNew = async () => {
    await nextTick();
    const chatContainer = document.querySelector(".chat");
    if (chatContainer) {
        chatContainer.scrollTop = chatContainer.scrollHeight;
    }
};
import axios from 'axios'
const sendQuestion = async () => { 
    console.log(value.value) 
    msgList.push({ type: '1', content:value.value});
  if (value.value.trim() !== '') {  
    axios({
        url:'https://apifoxmock.com/m2/4743995-0-default/193717866',
        // url:`${serverAddress}/apkUpload/aiChat?question=${value.value}`,
        method:'GET',
    }).then((response)=>{
        if(response.data.code===1){
            msgList.push({ type: '0', content:response.data.data });
        }else{
            msgList.push({ type: '0', content:'获取失败' });
        }
    }).catch((error)=>{
        msgList.push({ type: '0', content:'获取失败' });
    })
    value.value = '';
    scrollToNew(); 
    // // 假设这里是AI的响应  
    // setTimeout(() => {  
    //   msgList.push({ type: '1', content: `AI: ${value.value} is an interesting question!` });  
    //   value.value = ''; // 清空输入框  
    //   scrollToNew(); // 滚动到最新消息  
    // }, 1000);  
  }  
};  
</script>

<style scoped lang="scss">
.body {
    color: #fff;
    font-weight: 900;
    letter-spacing: 2px;
    width: 100%;
    height: 100%;
    background-size: 50%;
    display: flex;
    align-items: center;
    position: relative;
}

main {
    /* border: 1px solid red; */
    width: 1400px;
    height: 600px;
    margin: 100px auto;
    display: flex;
}

.cover {
    position: absolute;
    top: 0px;
    z-index: 0;
    height: 180px;
    width: 1483px;
    left: 50%;
    margin-left: -754px;
    overflow: hidden;
}

.body {
    :deep(.slick-slide) {
        text-align: center;
        height: 100%;
        line-height: 100%;
        background: #364d79;
        overflow: hidden;
    }

    :deep(.slick-arrow.custom-slick-arrow) {
        width: 25px;
        height: 25px;
        font-size: 25px;
        color: #fff;
        background-color: rgba(31, 45, 61, 0.11);
        transition: ease all 0.3s;
        opacity: 0.3;
        z-index: 1;
    }

    :deep(.slick-arrow.custom-slick-arrow:before) {
        display: none;
    }

    :deep(.slick-arrow.custom-slick-arrow:hover) {
        color: #fff;
        opacity: 0.5;
    }

    :deep(.slick-slide h3) {
        color: #fff;
    }
}

.container {
    z-index: 1;
    // border: solid 1px #bebebe;
    width: 85%;
    height: 100%;
    margin: -6px auto;
    display: flex;
    justify-content: center;

    .right {
        flex: 1;
        // border-radius: 10px;
        background-color: white;
        display: flex;
        flex-direction: column;
        height: 600px;

        .top {
            height: 50px;
            background-color: rgba(147, 213, 255, 0.764);
            width: 100%;
            font-size: 22px;
            text-align: center;
            line-height: 50px;
        }

        .chat {
            flex: 1;
            max-height: 700px;
            overflow-y: auto;
            padding: 10px;

            .leftMsg,
            .rightMsg {
                display: flex;
                flex-direction: row;
                justify-content: start;
                align-items: center;
                margin: 10px;

                img {
                    width: 40px;
                    height: 40px;
                    border-radius: 20px;
                    overflow: hidden;
                    object-fit: cover;
                    margin: 0 10px;
                }

                .msg {
                    display: inline-block;
                    padding: 10px;
                    word-wrap: anywhere;
                    max-width: 600px;
                    background-color: #364d79;
                    border-radius: 10px;
                }
            }

            .rightMsg {
                justify-content: end;

                .msg {
                    color: black;
                    background-color: #dfdfdf;
                }
            }
        }

        .bottom {
            height: 45px;
            display: flex;
            align-items: center;
            width: 80%;
            margin: 10px auto;

            input {
                width: 90%;
                border: 1px solid rgb(171, 171, 171);
                border-right: none;
                height: 40px;
                color: black;
                text-indent: 2px;
                line-height: 40px;
                border-radius: 10px 0 0 10px;
            }

            button {
                cursor: pointer;
                width: 10%;
                border: none;
                outline: none;
                height: 45px;
                border-radius: 0 10px 10px 0;
                background: linear-gradient(to right,
                        rgb(146, 197, 255),
                        rgb(200, 134, 200));
            }

            img {
                width: 20px;
                height: 20px;
            }
        }
    }
}

.separator {
    color: rgb(133, 132, 132);
    text-align: center;
    font-size: 15px;
    font-weight: normal;
}
.chat::-webkit-scrollbar{
    width:7px;
}
.chat::-webkit-scrollbar-track {  
    background: #bcbcbc;  
    border-radius: 5px;
}  
 
.chat::-webkit-scrollbar-thumb {  
    background: #595959; 
    border-radius: 5px;
}  
 
.chat::-webkit-scrollbar-thumb:hover {  
    background: #433f3f;  
}
</style>